package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.ErpCollectProductSplitLog;
import com.erp.gather.domain.bo.ErpCollectProductSplitLogBo;
import com.erp.gather.domain.vo.ErpCollectProductSplitLogVo;
import com.erp.gather.mapper.ErpCollectProductSplitLogMapper;
import com.erp.gather.service.IErpCollectProductSplitLogService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 采集商品拆分日志Service业务层处理
 *
 * @author erp
 * @date 2023-07-27
 */
@RequiredArgsConstructor
@Service
public class ErpCollectProductSplitLogServiceImpl implements IErpCollectProductSplitLogService {

    private final ErpCollectProductSplitLogMapper baseMapper;

    /**
     * 查询采集商品拆分日志
     */
    @Override
    public ErpCollectProductSplitLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询采集商品拆分日志列表
     */
    @Override
    public TableDataInfo<ErpCollectProductSplitLogVo> queryPageList(ErpCollectProductSplitLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpCollectProductSplitLog> lqw = buildQueryWrapper(bo);
        Page<ErpCollectProductSplitLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询采集商品拆分日志列表
     */
    @Override
    public List<ErpCollectProductSplitLogVo> queryList(ErpCollectProductSplitLogBo bo) {
        LambdaQueryWrapper<ErpCollectProductSplitLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpCollectProductSplitLog> buildQueryWrapper(ErpCollectProductSplitLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpCollectProductSplitLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCollectProductId() != null, ErpCollectProductSplitLog::getCollectProductId, bo.getCollectProductId());
        lqw.eq(bo.getSplitProductId() != null, ErpCollectProductSplitLog::getSplitProductId, bo.getSplitProductId());
        lqw.eq(bo.getType() != null, ErpCollectProductSplitLog::getType, bo.getType());
        lqw.eq(bo.getSplitTime() != null, ErpCollectProductSplitLog::getSplitTime, bo.getSplitTime());
        return lqw;
    }

    /**
     * 新增采集商品拆分日志
     */
    @Override
    public Boolean insertByBo(ErpCollectProductSplitLogBo bo) {
        ErpCollectProductSplitLog add = BeanUtil.toBean(bo, ErpCollectProductSplitLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改采集商品拆分日志
     */
    @Override
    public Boolean updateByBo(ErpCollectProductSplitLogBo bo) {
        ErpCollectProductSplitLog update = BeanUtil.toBean(bo, ErpCollectProductSplitLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpCollectProductSplitLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除采集商品拆分日志
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
